package com.alm.token.utils;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.hardware.Camera;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static java.lang.System.currentTimeMillis;

public class EthWalletUtil {

 static int   ADDRESS_LENGTH_IN_HEX=42;

    public static boolean isETHValidAddress(String input) {
        if (TextUtils.isEmpty(input) || !input.startsWith("0x"))
            return false;
        return EthWalletUtil.isValidAddress(input);
    }

    public static boolean isValidAddress(String input) {
        String cleanInput = EthWalletUtil.hexStringToString(input);

//        try {
//            Numeric.toBigIntNoPrefix(cleanInput);
//        } catch (NumberFormatException e) {
//            return false;
//        }

        if(TextUtils.isEmpty(cleanInput)){
            return false;
        }

        return input.length()==ADDRESS_LENGTH_IN_HEX;
    }



    /**
     * 16进制字符串转换为字符串
     *
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "gbk");
//            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
}
